//
//  UserViewController.m
//  product_B
//
//  Created by lanou on 16/6/2.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "UserViewController.h"
#import "UserTableHeaderView.h"
#import "UserModel.h"
#import "UserArticleViewController.h"
#import "UserKeepArticleViewController.h"
#import "MusicTimeTableViewController.h"
#import "NoteViewController.h"
#import "UserMovieViewController.h"
#import "UserPictureAndArticleViewController.h"
#import "RegistAndLoginViewController.h"
@interface UserViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UserTableHeaderView *userView;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UserModel *userModel;
@property (nonatomic,strong)NSMutableArray *cellArray1;
@property (nonatomic,strong)NSMutableArray *cellArray2;
@property (nonatomic,strong)UIImageView *imageView;
@end

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title= @"返回";
    if (_isShow) {
        self.cellArray1 = [@[@"图文",@"文章",@"影库",@"消息",@"图文.png",@"沙发.png",@"摄影机.png",@"消息.png"]mutableCopy];
        self.cellArray2 = [@[@"其他设置",@"注销账号",@"设置.png",@"注销.png"]mutableCopy];
        
        
    }else{
        self.cellArray1 = [@[@"ta的文章",@"ta的歌曲",@"文章.png",@"木歌单.png"]mutableCopy];
        self.cellArray2 = [@[@"图文",@"文章",@"影库",@"图文.png",@"沙发.png",@"摄影机.png"]mutableCopy];
    }
    
    [self requestWork];
    self.userView = [[UINib nibWithNibName:@"UserTableHeaderView" bundle:nil]instantiateWithOwner:self options:nil].firstObject;
    self.userView.backgroundColor = [UIColor clearColor];
    self.userView.userPhotoImageView.layer.cornerRadius = 30;
    self.userView.userPhotoImageView.layer.masksToBounds = YES;
    //    self.imageView = [[UIImageView alloc]initWithFrame:self.userView.bounds];
    self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -20, kWidth, 375)];
    self.imageView.image = [UIImage imageNamed:@"个人背景.png"];
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView addSubview:_imageView];
    [self.view addSubview:_tableView];
    
    
    //    self.tableView.backgroundColor = [UIColor clearColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 20, 30, 30);
    [button setImage:[UIImage imageNamed:@"取消.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self.userView.whiteImageView addGestureRecognizer:tap];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction1:)];
    [self.userView.musicFormImageView addGestureRecognizer:tap1];
    self.userView.whiteImageView.userInteractionEnabled = YES;
    self.userView.musicFormImageView.userInteractionEnabled = YES;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;
}
- (void)backButtonAction:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)requestWork{
    [RequestManager initWithUrlString:[NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/user/info/%@",_modelID] requestType:@"GET" parDic:nil finishBlock:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.userModel = [[UserModel alloc]init];
        [self.userModel setValuesForKeysWithDictionary:dic[@"data"]];
        [self.userView.userPhotoImageView sd_setImageWithURL:[NSURL URLWithString:self.userModel.web_url]];
        self.userView.userNameLabel.text = self.userModel.user_name;
        self.userView.scoreLabel.text = [NSString stringWithFormat:@"① %@",self.userModel.score];
        self.tableView.tableHeaderView = self.userView;
        dispatch_async(dispatch_get_main_queue(), ^{
            if (_isShow) {
                [self.userView.userPhotoImageView sd_setImageWithURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:@"picture"]]];
                self.userView.userNameLabel.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"name"];
                self.userView.scoreLabel.text = nil;
            }
            
            [self.tableView reloadData];
        });
        
    } errorBlock:^(NSError *error) {
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_isShow) {
        if (section == 0) {
            return 4;
        }else{
            return 2;
        }
    }else{
        if (section == 0) {
            if (self.userModel.isauthor.integerValue == 1) {
                return 2;
            }else{
                return 0;
            }
        }else{
            return 3;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    if (indexPath.section == 0) {
        cell.textLabel.text = _cellArray1[indexPath.row];
        cell.imageView.image = [UIImage imageNamed:_cellArray1[indexPath.row + self.cellArray1.count / 2]];
    }
    else{
        cell.textLabel.text = _cellArray2[indexPath.row];
        cell.imageView.image = [UIImage imageNamed:_cellArray2[indexPath.row + self.cellArray2.count / 2]];
        
    }
    cell.textLabel.textColor = [UIColor grayColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    }else{
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 50)];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 15, kWidth, 20)];
        if (_isShow) {
            label.text = @"设置";
        }else{
            label.text = @"ta的收藏";
        }
        
        label.textColor = [UIColor grayColor];
        [view addSubview:label];
        return view;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 50;
    }else{
        return 0;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_isShow) {
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                UserPictureAndArticleViewController *userPictureVC = [[UserPictureAndArticleViewController alloc]init];
                userPictureVC.modelID = _modelID;
                [self.navigationController pushViewController:userPictureVC animated:YES];
            }else if(indexPath.row == 1){
                UserKeepArticleViewController *keepVC = [[UserKeepArticleViewController alloc]init];
                keepVC.modelID = _modelID;
                [self.navigationController pushViewController:keepVC animated:YES];
            }else{
                UserMovieViewController *userMovieVC = [[UserMovieViewController alloc]init];
                userMovieVC.modelID = _modelID;
                [self.navigationController pushViewController:userMovieVC animated:YES];
            }
        }else{
            if (indexPath.row == 1) {
                [self alertAction];
            }
        }
    }else{
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                UserArticleViewController *uservc = [[UserArticleViewController alloc]init];
                uservc.modelID = _modelID;
                [self.navigationController pushViewController:uservc animated:YES];
            }else{
                MusicTimeTableViewController *music = [[MusicTimeTableViewController alloc]init];
                music.authorID = _modelID;
                [self.navigationController pushViewController:music animated:YES];
            }
        }else{
            if (indexPath.row == 0) {
                //            MusicTimeTableViewController *music = [[MusicTimeTableViewController alloc]init];
                //            music.musicID = _modelID;
                //            [self.navigationController pushViewController:music animated:YES];
                UserPictureAndArticleViewController *userPictureVC = [[UserPictureAndArticleViewController alloc]init];
                userPictureVC.modelID = _modelID;
                [self.navigationController pushViewController:userPictureVC animated:YES];
            }else if(indexPath.row == 1){
                UserKeepArticleViewController *keepVC = [[UserKeepArticleViewController alloc]init];
                keepVC.modelID = _modelID;
                [self.navigationController pushViewController:keepVC animated:YES];
            }else{
                UserMovieViewController *userMovieVC = [[UserMovieViewController alloc]init];
                userMovieVC.modelID = _modelID;
                [self.navigationController pushViewController:userMovieVC animated:YES];
            }
        }
    }
}
- (void)tapAction:(UITapGestureRecognizer *)tap{
    NoteViewController *noteVC =[[NoteViewController alloc]init];
    noteVC.modelId = _modelID;
    [self.navigationController pushViewController:noteVC animated:YES];
}
- (void)tapAction1:(UITapGestureRecognizer *)tap{
    MusicTimeTableViewController *music = [[MusicTimeTableViewController alloc]init];
    music.musicID = _modelID;
    [self.navigationController pushViewController:music animated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    if (scrollView.contentOffset.y<0) {
        self.imageView.frame = CGRectMake(scrollView.contentOffset.y/2, -20 + scrollView.contentOffset.y, kWidth - scrollView.contentOffset.y, 365 - scrollView.contentOffset.y);
    }else{
        self.imageView.frame = CGRectMake(0, -20 + scrollView.contentOffset.y, kWidth, 365 - scrollView.contentOffset.y);
    }
    
}

- (void)alertAction{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定注销?" preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:nil];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:cancel];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"name"];
        [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"picture"];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [alert addAction:sure];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
